package com.ruicar.afs.cloud.channel.register.task;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.channel.common.dto.CarAllListDTO;
import com.ruicar.afs.cloud.channel.common.dto.CarInfoListDTO;
import com.ruicar.afs.cloud.channel.common.dto.CarToListDTO;
import com.ruicar.afs.cloud.channel.common.entity.CarToList;
import com.ruicar.afs.cloud.channel.common.feign.ListBaseFeign;
import com.ruicar.afs.cloud.channel.register.entity.ChannelListingCarTemp;
import com.ruicar.afs.cloud.channel.register.service.ChannelListingCarTempService;
import com.ruicar.afs.cloud.channel.register.utils.RegisterDic;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.job.core.biz.model.ReturnT;
import com.ruicar.afs.cloud.common.job.core.handler.annotation.AfsJob;
import com.ruicar.afs.cloud.common.job.core.handler.annotation.AfsJobHandler;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.List;


@AfsJob
@AllArgsConstructor
@Slf4j
@Component
public class RegisterTask {
    private static final Logger logger = LoggerFactory.getLogger(RegisterTask.class);

    private ChannelListingCarTempService channelListingCarTempService;

    private final ListBaseFeign listBaseFeign;

    @Transactional(rollbackFor = Exception.class)
    @AfsJobHandler(value = "autoRegisterTask")
    public ReturnT<String> satrt(String params) {

        // 获取今天的毫秒数
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Long dayTime = calendar.getTime().getTime();

        // 查询出属于审核通过的挂靠车信息
        List<ChannelListingCarTemp> list = channelListingCarTempService.list(Wrappers.<ChannelListingCarTemp>query().lambda().eq(ChannelListingCarTemp::getStatus, RegisterDic.STATUS_PASS));
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                // 计算到期时间
                Long endTime = list.get(i).getApprovalTime().getTime() + list.get(i).getValidityDays() * 86400000;
                if (dayTime > endTime) {
                    // 今天的时间大于有效期截至日期，修改状态！
                    list.get(i).setStatus(RegisterDic.STATUS_STOP);
                    list.get(i).setStartStopReason("到期自动失效！");
                    channelListingCarTempService.updateById(list.get(i));

                    // 去名单库停用
                    CarInfoListDTO condition = new CarInfoListDTO();
                    condition.setPageNum(1);
                    condition.setPageSize(10);
                    condition.setVinNumber(list.get(i).getVin());
                    condition.setPartnerId(list.get(i).getChannelId().toString());
                    CarAllListDTO carVo = new CarAllListDTO();
                    carVo.setType("tb_car_info");
                    carVo.setData(condition);
                    JSONObject code = listBaseFeign.carListingSelecte(carVo);

                    // 看一看有没有数据
                    if ("200".equals(code.get("code").toString())) {
                        JSONObject data = code.getJSONObject("data");
                        Integer total = data.getInteger("total");
                        // 判断如果是1  就修改
                        if (total == 1) {
                            CarToList car = new CarToList();
                            car.setApiUser("apiUser");
                            car.setStatus("0");
                            // 声明一个参数封装类
                            List<CarToList> idss = data.getJSONArray("rows").toJavaList(CarToList.class);
                            car.setId(idss.get(0).getId());
                            CarToListDTO vo = new CarToListDTO();
                            vo.setType("tb_car_info");
                            vo.setData(car);
                            vo.setFileList(null);
                            // 这边是有需要修改
                            JSONObject json = listBaseFeign.carListingUpdate(vo);
                            if (!"200".equals(json.get("code").toString())) {
                                throw new AfsBaseException("更新名单库失败！");
                            }
                        }

                    }
                }
            }

        }
        return ReturnT.SUCCESS;
    }
}
